package io.renren.modules.usermanager.controller;

import cn.hutool.core.codec.Base64;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.modules.sys.dao.UserBaseInfoDao;
import io.renren.modules.sys.entity.UserBaseInfoEntity;
import io.renren.modules.usermanager.entity.UserManagerVo;
import io.renren.modules.usermanager.entity.dto.QueryUserCondition;
import io.renren.modules.usermanager.service.UsermanagerService;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Arrays;
import java.util.UUID;

@RestController
@RequestMapping("/usermanage/usermanagement")
public class UsermanagerController {

    @Autowired
    private UsermanagerService usermanagerService;
    @Autowired
    private UserBaseInfoDao userBaseInfoDao;

    /*后台接口 old 接口已经弃用*/
/*    @GetMapping("/list/{userId}/{current}/{size}")
    public R usermanageList(@PathVariable("userId")String userId,
                            @PathVariable("current")String current,
                            @PathVariable("size")String size){

        long userID = Long.parseLong(userId);


        R r = usermanagerService.getUserList(userID,current,size);
        return r;
    }*/

    /*用户管理查询后端接口*/
    @PostMapping("/list/{userId}/{current}/{size}")
    public R usermanageLists(@PathVariable("userId")String userId,
                            @PathVariable("current")String current,
                            @PathVariable("size")String size,
                            @RequestBody QueryUserCondition queryUserCondition){

        long userID = Long.parseLong(userId);


        R r = usermanagerService.getUserList(userID,current,size,queryUserCondition);
        return r;
    }






    /**
     * 修改
     */
    @PostMapping("/update")
    public R updateUserInfo(@RequestBody UserManagerVo userManagerVo){


        UserBaseInfoEntity userBaseInfoEntity = new UserBaseInfoEntity();

        BeanUtils.copyProperties(userManagerVo,userBaseInfoEntity);

        int isUpdate = userBaseInfoDao.updateById(userBaseInfoEntity);
        if (isUpdate > 0){
            return R.ok();
        }else{
            return R.error();
        }
    }



    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        R r = usermanagerService.removeUserInfo(Arrays.asList(ids));
        return r;
    }


    /*用户上传头像*/
    @PostMapping("/userUploadAdvator/{userId}")
    public R uploadUserAdvator(@PathVariable("userId")String userId, MultipartFile photo, HttpSession session){
        //获取上传的文件的文件名
        String fileName = photo.getOriginalFilename();

        //处理文件重名问题
        String hzName = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID().toString() + hzName;

        String photoPath = "E:\\useradvator\\useradvator\\";

        String finalPath = photoPath + File.separator + fileName;
        //实现上传功能
        try {
            photo.transferTo(new File(finalPath));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.ok().put("pathUrl",finalPath);
    }

    /*用户头像反序列化*/
    @PostMapping("/reflectUserAdvator/{userId}")
    public R reflectUserAdvator(@PathVariable("userId")String userId){

        QueryWrapper<UserBaseInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_info_id",userId);
        UserBaseInfoEntity userBaseInfoEntity = userBaseInfoDao.selectOne(wrapper);

        if (userBaseInfoEntity==null || userBaseInfoEntity.getAdvator() == null){
            return R.ok();
        }

        String advatorPath = userBaseInfoEntity.getAdvator();

        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(new File(advatorPath));
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok().put("data","data:image/jpeg;base64,"+ Base64.encode(data));
    }


}
